import { Component, OnInit ,OnDestroy} from '@angular/core';
import { Router ,RouteParams} from '@angular/router-deprecated';
import { Group } from '../model/group.model';
import { Api } from '../model/api.model';
import { Proj } from '../model/proj.model';
import {BaseRequestOptions,Http,Response, HTTP_PROVIDERS,Headers} from '@angular/http';
import { GlobalService } from '../services/global.service';
import {ProjService} from '../services/proj.service';
import { GroupService } from '../services/group.service';

@Component({
  selector: 'edit-group-component',
  viewProviders: [HTTP_PROVIDERS,GroupService,ProjService],
  templateUrl: 'app/group/tpl/edit.group.tpl.html'
})
export class GroupEditComponent implements OnInit,OnDestroy {

  proj:Proj = new Proj();
  group:Group = new Group();
  constructor(private router: Router,routeParams:RouteParams,private projService: ProjService,private groupService:GroupService) {
    projService.authInit(this.router,()=>{
      let projId:string = routeParams.get("projId")
      if(projId)
      {
        this.projService.getProj(projId).subscribe(data=>this.proj = data.json());
      }
      let groupId:string = routeParams.get("groupId")
      if(groupId)
      {
        this.groupService.get(groupId).subscribe(data=>{
          this.group = data.json()
          this.proj = this.group['project'];
        });
      }
    });
  }

  ngOnInit() {
  }

  ngOnDestroy(){

  }

  submit()
  {
    this.group.projectId = this.proj._id;
    if(this.group._id === undefined || this.group._id ==="" || this.group._id===null)
    {
      this.groupService.add(this.group).subscribe(data=>this.router.navigate( ['Group',{projId:this.proj._id}]));
    }else
    {
      this.groupService.update(this.group).subscribe(data=>this.router.navigate( ['Group',{projId:this.proj._id}]));
    }
  }
  delete()
  {
    $('.ui.basic.modal')['modal']('show');
  }

  confirmDelete()
  {
    this.groupService.delete(this.group._id).subscribe();
  }

}
